package com.woniuxy.controller;


import com.woniuxy.entitys.TbWorker;

import com.github.pagehelper.PageInfo;
import com.woniuxy.entitys.TbRole;
import com.woniuxy.entitys.TbSection;
import com.woniuxy.entitys.TbWorker;
import com.woniuxy.services.RoleService;

import com.woniuxy.services.WorkerService;
import com.woniuxy.utils.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.woniuxy.entitys.TbUser;
import com.woniuxy.entitys.TbWorker;
import com.woniuxy.services.WorkerService;
import com.woniuxy.utils.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class WorkerController {

    @Autowired
    private WorkerService workerService;

    @GetMapping("/fixWorkerList")
    public ResultVO getFixWorkerList() {
        List<TbWorker> fixWorkerList = workerService.getFixWorkerList();
        return fixWorkerList != null ? ResultVO.success("success", fixWorkerList) : ResultVO.failure("failed");
    }

    @Autowired
    private RoleService roleService;

    @PostMapping("/worker/getSalary")
    public ResultVO getSalary(@RequestBody Map<String,Object> map){
        Integer pageNum=(Integer) map.get("pageNum");
        Integer pageSize=(Integer) map.get("pageSize");
        String phone=(String)map.get("phone");
        String uname=(String)map.get("uname");
        Integer rid=null;
        if(map.get("rid")!=null&&map.get("rid")!=""){
            rid = (Integer)map.get("rid");
        }
        PageInfo<TbWorker> tbWorker = workerService.getTbWorker(pageNum, pageSize, uname, phone, rid);
        return tbWorker!=null?ResultVO.success("查询成功",tbWorker):ResultVO.failure("查询失败");
    }
    @GetMapping("/role/getallrole")
    public ResultVO getallRole(){
        List<TbRole> getallrole = roleService.getallrole();
        return getallrole!=null?ResultVO.success("查询成功",getallrole):ResultVO.failure("查询失败");
    }
    @PostMapping("/worker/list")
    public ResultVO getAllWorkers(@RequestBody Map<String,Object> map){
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        PageInfo<TbWorker> pageInfo = workerService.selAllWorkers(pageNum, pageSize);
        return pageInfo!=null?ResultVO.success("success",pageInfo):ResultVO.failure("error");
    }
    @PostMapping("/worker/condition")
    public ResultVO getWorkerByCondition(@RequestBody Map<String,Object> map){
        Integer pageSize = (Integer) map.get("pageSize");
        Integer pageNum = (Integer) map.get("pageNum");
        String keyword = (String) map.get("keyword");
        PageInfo<TbWorker> pageInfo = workerService.selWokersByCondition(pageSize,pageNum,keyword);
        return pageInfo!=null?ResultVO.success("success",pageInfo):ResultVO.failure("error");
    }

    @PostMapping("/worker/delWorker/{wid}")
    public ResultVO softdelWorker(@PathVariable("wid") Integer wid){
        int i = workerService.updateSoftdel(wid);
        return i>0?ResultVO.success("success"):ResultVO.failure("error");
    }

    @GetMapping("/loadWorkerInfo")
    public ResultVO loadWorkerInfo(){
        TbWorker worker = workerService.loadWorkerInfo();
        return worker!=null?ResultVO.success("查询成功",worker):ResultVO.failure("查询失败");
    }

    @PostMapping("/updWorkerInfo")
    public ResultVO updWorkerInfo(@RequestBody TbWorker worker){
        int i = workerService.updWorkerInfo(worker);
        return i>0?ResultVO.success("success"):ResultVO.failure("error");
    }

}
